Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change endpoint and update Jest configuration and tests #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

yryuvraj
Copy link
Member

#5

src/index.ts Outdated Show resolved Hide resolved

import { generateCronExpression, generateReadableExpression } from '../src/cronGenerator';

app.get('/v1/generate', (req, res) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do not duplicate server code! (remember, DRY (don't repeat yourself)

Refer to the guide mentioned in jest docs.

__tests__/index.test.ts Show resolved Hide resolved

app.use('/', routes);

app.listen(port, () => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. export app from this file
  2. in index.ts, start the actual server by importing app and doing app.listen(). This allows you to import the app in your test framework for E2E testing as well.

export function generateReadableExpression(cronExpression: string): string {
try {
return cronstrue.toString(cronExpression);
} catch (error) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't try catch here, let it throw error. It should be automatically handled by the global express error catcher and accordingly return an error.


export function generateCronExpression() {
function getRandomOrWildcard(min: number, max: number) {
if (Math.random() < 0.7) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like Math.random cannot be seeded, so we can't really unit test this part of code. What we can do, however, is use property based testing to fuzzy test the generator and verify if the output is always valid.

We can check validity by passing the output to generadeReadableExpression and expect it to NOT throw.

__tests__/index.test.ts Show resolved Hide resolved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants